using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ProtoGame.GameLib.GameComponents;
using ProtoGame.GameLib;


namespace ProtoGame.Fases
{
    public enum EstadosFase
    {
        Correndo,
        Pausado,
        JogadorMorreu,
        FaseCompletada
    }

    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public abstract class PGFase<TIdFase> : Microsoft.Xna.Framework.DrawableGameComponent where TIdFase : struct
    {
        public event EventHandler FimFase;

        protected EntityManager Mapa { get; set; }

        public EstadosFase EstadoFase { get; protected set; }

        public TIdFase Id { get; set; }

        public PGFase(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            DialogBox<byte> db = Game.Services.GetService(typeof(DialogBox<byte>)) as DialogBox<byte>;
            db.Finished += new EventHandler(db_Finished);

            AtributosJogador jogador = Game.Services.GetService(typeof(AtributosJogador)) as AtributosJogador;
            jogador.EnergiaAcabou += new EventHandler(jogador_EnergiaAcabou);

            PGHUD hud = Game.Services.GetService(typeof(PGHUD)) as PGHUD;
            hud.WakeUp();

            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            DialogBox<byte> db = Game.Services.GetService(typeof(DialogBox<byte>)) as DialogBox<byte>;
            db.Finished -= db_Finished;

            AtributosJogador jogador = Game.Services.GetService(typeof(AtributosJogador)) as AtributosJogador;
            jogador.EnergiaAcabou -= jogador_EnergiaAcabou;

            base.Dispose(disposing);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (EstadoFase == EstadosFase.Correndo)
                Mapa.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Mapa.Draw(gameTime);

            base.Draw(gameTime);
        }

        protected virtual void OnFimFase(object sender, EventArgs e)
        {
            PGHUD hud = Game.Services.GetService(typeof(PGHUD)) as PGHUD;
            hud.GoSleep();

            if (FimFase != null)
                FimFase(sender, e);
        }

        protected virtual void jogador_EnergiaAcabou(object sender, EventArgs e)
        {
            EstadoFase = EstadosFase.JogadorMorreu;

            DialogBox<byte> db = Game.Services.GetService(typeof(DialogBox<byte>)) as DialogBox<byte>;
            db.AddSpeech("Atalph: \"Nao posso mais suportar meus ferimentos... uhnnn...\"");
            db.WakeUp();
        }

        void db_Finished(object sender, EventArgs e)
        {
            EstadoFase = EstadosFase.Correndo;
        }
    }
}